package com.yks.pls;


import com.yks.pls.util.DefaultRedisProvider;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * JMX 执行器
 * @ClassName: com.yks.pls.JMXSpringExecutor.java
 * @author: songxulin
 * @date :  2021-01-14 17:43
 * @version V1.0
 */
public class JMXSpringExecutor extends DefaultRedisProvider implements ApplicationContextAware, SmartInitializingSingleton, DisposableBean {
    ScheduledExecutorService timer = new ScheduledThreadPoolExecutor(1,new ThreadPoolExecutor.DiscardPolicy());
    private ApplicationContext applicationContext;

    private long period = 5000;

    @Override
    public void destroy() throws Exception {

    }

    @Override
    public void afterSingletonsInstantiated() {
        timer.scheduleAtFixedRate(() -> {
            System.out.println("你好");
        }, 1000, period, TimeUnit.MILLISECONDS);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void setPeriod(long period) {
        this.period = period;
    }
}
